package com.iflytek.yly.mvvmdemo.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import com.iflytek.yly.mvvmdemo.MyApplication;
import com.iflytek.yly.mvvmdemo.db.UserDao;
import com.iflytek.yly.mvvmdemo.db.UserDatabase;
import com.iflytek.yly.mvvmdemo.model.User;
import com.iflytek.yly.mvvmdemo.repository.UserRepository;

public class UserViewModel extends AndroidViewModel {

    private LiveData<User> user;
    private UserRepository userRepository;
    private String userName = "MichaelYe";

    public UserViewModel(@NonNull Application application) {
        super(application);
        UserDatabase database = MyApplication.getUserDatabase();
        UserDao userDao = database.userDao();
        userRepository = new UserRepository(userDao,MyApplication.getApi());
        user = userRepository.getUser(userName);
    }

    public LiveData<User> getUser() {
        return user;
    }

    public void refresh(){
        userRepository.refresh(userName);
    }

}
